home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / QT2MPEG.ZIP / mkwav.diff < prev    next >
Text File  |  1994-12-27  |  3KB  |  90 lines

  1. #include <stdio.h>
  2.  
  3. typedef unsigned long DWORD;
  4. typedef unsigned short WORD;
  5.  
  6. /* this header structure comes from a wav play/record program by 
  7.    Andre Fuechsel (af1@irz.inf.tu-dresden.de), but he used code from 
  8.    Liam Corner, so this might be his too.  */
  9.    
  10. typedef struct {                /* header for WAV-Files */
  11.         char main_chunk[4];     /* 'RIFF' */
  12.         DWORD length;           /* length of file */
  13.         char chunk_type[4];     /* 'WAVE' */
  14.         char sub_chunk[4];      /* 'fmt' */
  15.         DWORD length_chunk;     /* length sub_chunk, always 16 bytes */
  16.         WORD format;            /* always 1 = PCM-Code */
  17.         WORD modus;             /* 1 = Mono, 2 = Stereo */
  18.         DWORD sample_fq;        /* Sample Freq */
  19.         DWORD byte_p_sec;       /* Data per sec */
  20.         WORD byte_p_spl;        /* bytes per sample, 1=8 bit, 2=16 bit (mono)
  21.                                                      2=8 bit, 4=16 bit (stereo) */
  22.         WORD bit_p_spl;         /* bits per sample, 8, 12, 16 */
  23.         char data_chunk[4];     /* 'data' */
  24.         DWORD data_length;      /* length of data */
  25. } wave_header;
  26.  
  27. FILE *in= 0, *out= 0;
  28. char *buf;
  29. char infile[80], outfile[80];
  30. int data_size, tmp, i;
  31. wave_header head;
  32.  
  33. main(int argc, char **argv) {
  34.   if (argc> 1) { 
  35.     strcpy(infile, argv[1]);  
  36.     in= fopen(infile, "rb");
  37.     if (!in) printf("Unable to open %s\n", infile);
  38.     else printf("Input: %s\n",infile); 
  39.   }
  40.   while (!in) {
  41.     printf("Input file: ");
  42.     fgets(infile, 80, stdin);
  43.     infile[strlen(infile)-1]= 0;
  44.     if (!infile[0]) exit(1);
  45.     in= fopen(infile, "rb");
  46.     if (!in) printf("Unable to open %s\n", infile);
  47.   }
  48.   if (argc> 2) strcpy(outfile, argv[2]); 
  49.   else {
  50.     for (i=strlen(infile);infile[i]!='.'  &&  i; i--) ;
  51.     if (i) infile[i]= 0;
  52.     strcpy(outfile, infile);
  53.     strcat(outfile, ".wav");
  54.   }
  55.   out= fopen(outfile, "wb");
  56.   if (!out) { printf("Unable to open %s\n", outfile); exit(1); }
  57.   printf("Output: %s\n",outfile);
  58.  
  59.   fseek(in, 0, SEEK_END);
  60.   buf = (char*) malloc(data_size= ftell(in));
  61.   fseek(in, 0, SEEK_SET);
  62.   fread(buf, 1, data_size, in);
  63.   fclose(in);
  64.  
  65.   strncpy(head.main_chunk, "RIFF", 4);
  66.   strncpy(head.chunk_type, "WAVE", 4);
  67.   strncpy(head.sub_chunk, "fmt ", 4);
  68.   strncpy(head.data_chunk, "data", 4);
  69.   head.length_chunk = 16;
  70.   head.format = 1;
  71.   printf("# of channels: ");
  72.   scanf("%d",& head.modus);
  73.   printf("Sample Rate: ");
  74.   scanf("%u", &head.sample_fq);
  75.   printf("Bits Per Sample: ");
  76.   scanf("%u",& tmp);
  77.   head.bit_p_spl = tmp;
  78.   head.byte_p_spl = (tmp >> 3)* head.modus;
  79.   head.byte_p_sec = head.sample_fq * head.byte_p_spl;
  80.   head.data_length =  data_size;
  81.   head.length = head.data_length + 36;
  82.   if (fwrite(&head, 1, sizeof(head), out) != sizeof(head)) {
  83.     printf("error writing header\n");
  84.     exit(1);
  85.   }
  86.   fwrite(buf, 1, data_size, out);
  87.   fclose(out);
  88. }
  89.  
  90.